package profile

import (
	"context"
	"errors"

	"usercenter/internal/middleware"
	"usercenter/internal/svc"
	"usercenter/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateJoyInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateJoyInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateJoyInfoLogic {
	return &UpdateJoyInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateJoyInfoLogic) UpdateJoyInfo(req *types.JoyInfoRequest) (resp *types.JoyInfoResponse, err error) {
	userId, ok := l.ctx.Value(middleware.UserIdKey).(int)
	if !ok || userId == 0 {
		return nil, errors.New("未授权访问: 获取用户ID失败或无效")
	}

	joyimg, err := l.svcCtx.UserModel.UpdateJoyInfo(userId, req.JoyName, req.JoyTitle, req.JoyId)
	if err != nil {
		return nil, errors.New("更新失败" + err.Error())
	}
	resp = &types.JoyInfoResponse{
		Code:    200,
		Message: "更新成功",
		Data: types.JoyUserInfo{
			JoyImgURL: joyimg.JoyImgUrl,
			JoyName:   req.JoyName,
			JoyTitle:  req.JoyTitle,
		},
	}
	return resp, nil
}
